/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ServerGUI;
import java.io.ObjectInputStream;
import Communication.S2ModulePacket;


/**
 *
 * @author cypek
 */
public class ServerGUIListner implements Runnable {

    private ServerGUIController guiController;
    private S2ModulePacket inpacket;

    

    public ServerGUIListner(ServerGUIController guiController) {
        this.guiController = guiController;

    }

    public ServerGUIController getGuiController() {
        return guiController;
    }

    public void setGuiController(ServerGUIController guiController) {
        this.guiController = guiController;
    }

    /**
     * Get the value of inpacket
     *
     * @return the value of inpacket
     */
    public S2ModulePacket getInpacket() {
        return inpacket;
    }

    /**
     * Set the value of inpacket
     *
     * @param inpacket new value of inpacket
     */
    public void setInpacket(S2ModulePacket inpacket) {
        this.inpacket = inpacket;
    }



    @Override
    public void run()
    {
        try {

              while(guiController.isIsServerGUIRunning())
                {
                    ObjectInputStream ois = new ObjectInputStream(this.guiController.getSocket().getInputStream());
                    Object readObject = ois.readObject();

                    if ( readObject instanceof S2ModulePacket ){
                            inpacket = (S2ModulePacket)readObject;
                            this.guiController.setCurrentFloor(inpacket.getCurrentFloor());
                            this.guiController.setCurrentDirection(inpacket.getCurrentDirection());
                            this.guiController.setClientLists(inpacket.getClientList());

//                            System.out.println("Lista waitingQueue \t" + inpacket.getClientLists().getWaitingQueue().getClientList().size());
//                            System.out.println("Lista freeQueue \t " + inpacket.getClientLists().getConnectedQueue().getClientList().size());
                            Thread.sleep(Const.ModuleConst.TimeDelayShort);
                    }
                }
            }
         catch (Exception ex) {
               System.out.println("Blad podczas nasluchiwania");
               System.out.println( "class " + ex.getClass() + " \t " + ex.getMessage() );
          }

    }
}
